<template>
  <el-dialog
    :title="!dataForm.merchantId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商户登录名" prop="loginName">
      <el-input v-model="dataForm.loginName" placeholder="商户登录名"></el-input>
    </el-form-item>
    <el-form-item label="登录密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="登录密码"></el-input>
    </el-form-item>
    <el-form-item label="商户电话" prop="mobilePhone">
      <el-input v-model="dataForm.mobilePhone" placeholder="商户电话"></el-input>
    </el-form-item>
    <el-form-item label="商铺名" prop="merchantName">
      <el-input v-model="dataForm.merchantName" placeholder="商铺名"></el-input>
    </el-form-item>
    <el-form-item label="商户真实姓名" prop="merchantRealName">
      <el-input v-model="dataForm.merchantRealName" placeholder="商户真实姓名"></el-input>
    </el-form-item>
    <el-form-item label="商户头像" prop="userImg">
      <el-input v-model="dataForm.userImg" placeholder="商户头像"></el-input>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          merchantId: 0,
          loginName: '',
          password: '',
          mobilePhone: '',
          merchantName: '',
          merchantRealName: '',
          userImg: ''

        },
        dataRule: {
          loginName: [
            { required: true, message: '商户登录名不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '登录密码不能为空', trigger: 'blur' }
          ],
          mobilePhone: [
            { required: true, message: '商户电话不能为空', trigger: 'blur' }
          ],
          merchantName: [
            { required: true, message: '商铺名不能为空', trigger: 'blur' }
          ],
          merchantRealName: [
            { required: true, message: '商户真实姓名不能为空', trigger: 'blur' }
          ],
          userImg: [
            { required: true, message: '商户头像不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.merchantId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.merchantId) {
            this.$http({
              url: this.$http.adornUrl(`/shop/merchant/info/${this.dataForm.merchantId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.loginName = data.merchant.loginName
                this.dataForm.password = data.merchant.password
                this.dataForm.mobilePhone = data.merchant.mobilePhone
                this.dataForm.merchantName = data.merchant.merchantName
                this.dataForm.merchantRealName = data.merchant.merchantRealName
                this.dataForm.userImg = data.merchant.userImg
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/shop/merchant/${!this.dataForm.merchantId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'merchantId': this.dataForm.merchantId || undefined,
                'loginName': this.dataForm.loginName,
                'password': this.dataForm.password,
                'mobilePhone': this.dataForm.mobilePhone,
                'merchantName': this.dataForm.merchantName,
                'merchantRealName': this.dataForm.merchantRealName,
                'userImg': this.dataForm.userImg
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
